'''
Author: wangyingjie 2778809626@qq.com
Date: 2025-07-03 15:35:52
LastEditors: wangyingjie 2778809626@qq.com
LastEditTime: 2025-07-18 15:32:00
FilePath: /two_wheeled_car/src/bot_description/launch/bot_visualize.launch.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.substitutions import Command
from launch_ros.actions import Node

# 用于加载机器人模型并启动robot_state_publisher节点

def generate_launch_description():

    # 定义URDF文件的路径和包描述
    urdf_file = 'urdf/bot.urdf'
    package_desc = 'bot_description'
    
    print("URDF LOADING..")
    
    # 获取机器人描述路径
    robot_desc_path = os.path.join(get_package_share_directory(package_desc), urdf_file)
    
    # 配置robot_state_publisher节点
    # 创建并配置一个用于发布机器人状态的节点
    robot_state_publisher_node = Node(
        package='robot_state_publisher',  # 指定节点所属的包名
        executable='robot_state_publisher',  # 指定节点运行的可执行文件
        name='robot_state_publisher_node',  # 为节点指定一个名称
        emulate_tty = True,  # 设置节点以模拟终端模式运行，以便于调试和日志输出
        parameters=[{ 
            'use_sim_time': True,  # 使用模拟时间，这通常用于仿真环境中
            'robot_description': Command(['xacro ', robot_desc_path])  # 通过xacro命令解析机器人描述文件路径，并将结果作为参数传递
        }],
        output = "screen"  # 将节点的输出重定向到屏幕，便于实时查看日志和调试信息
    )
    
    # 定义RVIZ配置文件路径
    # rviz_config_dir = os.path.join(get_package_share_directory(package_desc), 'rviz')
    rviz_config_file = os.path.join(get_package_share_directory(package_desc), 'rviz', 'bot.rviz')

    # 配置rviz2节点
    rviz_node = Node(
        package='rviz2',
        executable='rviz2',
        output='screen',
        name='rviz_node',
        parameters=[{'use_sim_time': True}],
        arguments=['-d', rviz_config_file])
    
    # 返回LaunchDescription对象，包含所有要启动的节点
    return LaunchDescription(
        [
            robot_state_publisher_node,
            rviz_node
        ]
    )